﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging
// File Name             :  Logger.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      July 01, 2013    Initial Version
// =============================================================================================
#endregion

#region Using Region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using System.Configuration;

#endregion
namespace ICE.Vacations.Helper
{
    public static class Logger
    {

        /// <summary>
        /// Decode Error codes.
        /// </summary>
        /// <param name="ErrorType"></param>
        /// <returns></returns>
        public static String ErrorMessage(int ErrorType)
        {
            switch (ErrorType)
            {
                case 0:
                    return "Error Message";
                case 1:
                    return "Log Message";
                case 2:
                    return "Information Message";
                case 3:
                    return "Proccess Message";
                default:
                    return "Error de configuracion: Error Type solo puede ser de 0 - 3. Valor Acutal: " + ErrorType;
            }
        }


        /// <summary>
        /// Determine if write depending of level log.
        /// </summary>
        /// <param name="errorType"></param>
        /// <returns></returns>
        public static bool ifWrite(int errorType)
        {

            switch (errorType)
            {
                case 0:
                    {
                        return Configuration.ConfigurationError;
                    }
                case 1:
                    {
                        return Configuration.DBError;
                    }
                case 2:
                    {
                        return Configuration.InformationMessage;

                    }
                case 3:
                    {
                        return Configuration.SuccessMessage;
                    }
                default:
                    {
                        throw new Exception("Error del sistema: el tipo de error solo puede ser de 0 - 3. Valor Acutal: " + errorType);
                    }
            }
            /*}
            catch (Exception e)
            {
                throw new Exception("Error al intentar las llaves de bitacora: " + e.Message);
            }*/
        }



        public static bool ifFileWrite()
        {
            return true;

        }



        /// <summary>
        /// Write Logs in File System
        /// </summary>
        /// <param name="message"></param>
        /// <param name="appConfig"></param>
        public static void fileWrite(string message)
        {
            int _logCounter = 0;
            string logFilename = Configuration.logLocation + "\\" + Configuration.logPrefix + "-" + DateTime.Now.ToString("yyyyMMdd") + ".Log";

            if (!(Directory.Exists(Configuration.logLocation)))
                Directory.CreateDirectory(Configuration.logLocation);
            try
            {
                using (StreamWriter swLocalLog = new StreamWriter(logFilename, true))
                {
                    swLocalLog.WriteLine(message);
                    swLocalLog.Flush();
                }

                FileInfo fileProps = new FileInfo(logFilename);
                if (fileProps.Length > (Convert.ToInt32(Configuration.logMaxSizeBytes) * 1000))
                {
                    while (true)
                        try
                        {
                            File.Move(logFilename, logFilename + _logCounter.ToString() + ".old");
                            break;
                        }
                        catch { _logCounter++; }
                }
            }

            catch (Exception exc)
            {
                throw new Exception("Imposible escribir en el logFile " + exc.Message);
            }

        }

        /// <summary>
        /// Write events to Windows Event Viewer, reading configurations file for the application.
        /// </summary>
        /// <param name="msgText">Text to be written to the log.</param>
        public static void LogThis4Windows(string msgText, string _eventSourceName, string _eventLogName)
        {
            try
            {
                EventLog _myServiceWindowsLog = new EventLog();
                if (!EventLog.SourceExists(_eventSourceName))
                {
                    EventLog.CreateEventSource(_eventSourceName, _eventLogName);
                }
                _myServiceWindowsLog.Log = _eventLogName;
                _myServiceWindowsLog.Source = _eventSourceName;
                _myServiceWindowsLog.WriteEntry(msgText, EventLogEntryType.Error);
            }
            catch (Exception exc)
            {
            }
        }
    }

}